package com.centriole.service.workflow.controller;

import com.centriole.service.workflow.entity.ProParameter;
import com.centriole.service.workflow.service.ProParameterService;
import com.centriole.service.workflow.service.ProcessService;
import com.centriole.service.workflow.util.AjaxResult;
import com.centriole.service.workflow.util.CommonUtil;
import com.centriole.service.workflow.util.ResultJson;
import com.centriole.service.workflow.util.UserUtils;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/flow/processParameter")
public class ProcessParameterController {
	@Resource
	private ProParameterService proParameterService;
	@Resource
    ProcessService processService;
	/**
     * 新增流程参数
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(ProParameter proParameter, HttpServletRequest request) {
      //  try {
        String creatorId = proParameter.getCreatorId();
        creatorId = StringUtils.isBlank(creatorId)?UserUtils.currentUserInfo().getEmpId()+"":creatorId;
        proParameter.setCreatorId(creatorId);
        proParameter.setModifierId(creatorId);
        proParameterService.insertParameter(proParameter);
            return AjaxResult.Success("保存成功", proParameter.getPk());
     //   } catch (Exception e) {
     //       return AjaxResult.Error(e.getMessage());
     //   }
    }
    
    /**
     * 删除流程参数
     * @param ids
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(@RequestParam(value = "ids", required = false, defaultValue = "") String[] ids, HttpServletRequest request) {
        try {
        	for(String id:ids){
        		proParameterService.logicDeleteByProcessId(id);
        	}
            return AjaxResult.Success("成功");
        } catch (Exception ce) {
            return AjaxResult.Error(ce.getMessage());
        }
    }
    
    /**
     * 提供json格式数据
     *
     * @param sortName  排序属性
     * @param sortOrder 排序类型 {asc, desc}
     * @param currPage  当前页
     * @param pageSize  页大小
     * @param request   request
     */
    @ResponseBody
    @RequestMapping("/list/json")
    public ResultJson listJson(@RequestParam(value = "sortname", required = false, defaultValue = "") String sortName,
                           @RequestParam(value = "sortorder", required = false, defaultValue = "") String sortOrder,
                           @RequestParam(value = "page", required = false, defaultValue = "") String currPage,
                           @RequestParam(value = "pagesize", required = false, defaultValue = "") String pageSize,
                           HttpServletRequest request) {

//    	Map<String,Object> obj = new HashMap<String,Object>();
        try {
            List<ProParameter> list = proParameterService.select();
//            if (list != null) {
//            	obj.put("Rows", list);
//            }
            return ResultJson.usePageInfo(new PageInfo<ProParameter>(list));
        } catch (Exception e) {
            return ResultJson.error(ResultJson.INNER_ERR_MSG);
//            BapLogUtils.logError(JsonConstant.SEARCH_DATA_ERROR, e);
        }

    }
    
    /**
     * 获取参与判断的参数数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getJudgeData")
    public Object getJudgeData(HttpServletRequest request) {
        String processId = request.getParameter("processId");
        Map<String,Object> obj = new HashMap<String,Object>();
        try {
            List<ProParameter> list = proParameterService.selectByProcessId(processId);
            if (list != null) {
            	obj.put("Rows", list);
            }
            return obj;
        } catch (Exception e) {
//            BapLogUtils.logError(JsonConstant.SEARCH_DATA_ERROR, e);
        }
        
        return "SYS.ERROR";
    }
    
    /**
     * 流程参数列表页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "flowpages/processParameterList";
    }
    
    /**
     * 流程参数编辑页面
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response, String id) {
    	if(CommonUtil.isNotEmpty(id)){
			model.addAttribute("process", JSONObject.fromObject(proParameterService.selectById(id)));
		}else{
			model.addAttribute("process", JSONObject.fromObject(new ProParameter()));
		}
    	model.addAttribute("processList", JSONArray.fromObject(processService.select("")));
        return "flowpages/processParameterEdit";
    }
    
    /**
     * 启动条件
     * @return
     */
    @RequestMapping("/addStartCondition")
    public ModelAndView startConditions(@RequestParam(value = "processId", required = false, defaultValue = "") String processId) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/flowpages/startConditions");//设置页面
        mav.addObject("processId",processId);
        return mav;
    }
    
    /**
     * 输出条件
     * @return
     */
    @RequestMapping("/conditions")
    public ModelAndView conditions(@RequestParam(value = "processId", required = false, defaultValue = "") String processId) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/flowpages/conditions");//设置页面
        mav.addObject("processId",processId);
        return mav;
    }
}
